package ac.kr.ml.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ac.kr.ml.vo.FriendVo;
import ac.kr.ml.vo.UserVo;

public class UserDao {
	
String LOCALHOST = "partner.medialog.co.kr";
	
	
	public List<FriendVo> getFriend(int key){
		
		List<FriendVo> list = new ArrayList<FriendVo>();

		
		Connection conn = null;
		Statement stmt= null;

		try {
			Class.forName("com.mysql.jdbc.Driver");

			String dburl = "jdbc:mysql://"+LOCALHOST+":3306/freshman";
			conn = DriverManager.getConnection(dburl, "freshman", "freshman!$");


			stmt = conn.createStatement();

			String sql = "select * from Friend where empID='"+key+"'";

			ResultSet rs = stmt.executeQuery(sql);

			while(rs.next()){
				FriendVo vo = new FriendVo();
				vo.setEmpID(Integer.parseInt(rs.getString(1)));
				vo.setFmpID(Integer.parseInt(rs.getString(2)));				
				list.add(vo);				
			}
			if( list.size() ==0){
				list = null;
			}

			rs.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException ex){
			ex.printStackTrace();
		} finally{
			try{
				stmt.close();
			}catch(SQLException e){}
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException e){}
			}

		}
		return list;
	}
	
	
public UserVo getUser(String key){
		
		UserVo vo = new UserVo();

		int empID =0;
		
		Connection conn = null;
		Statement stmt= null;

		try {
			Class.forName("com.mysql.jdbc.Driver");

			String dburl = "jdbc:mysql://"+LOCALHOST+":3306/freshman";
			conn = DriverManager.getConnection(dburl, "freshman", "freshman!$");


			stmt = conn.createStatement();

			String sql = "select empID from addressinfo where userID='"+key+"'";

			ResultSet rs = stmt.executeQuery(sql);

			while(rs.next()){
				vo.setKey(rs.getInt(1));				
			}
			empID = vo.getKey();
			if(empID<=0){
				vo = null;
			}

			rs.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException ex){
			ex.printStackTrace();
		} finally{
			try{
				stmt.close();
			}catch(SQLException e){}
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException e){}
			}

		}		
		return vo;
	}

}
